package ae.pcfc.cldos.online.web.services.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;

import ae.pcfc.cldos.online.web.services.request.beans.RequestVariablesBean;

/**
 * 
 * @author arosa
 *
 */
public class CustomLoginUrlAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
	
	@Value("${login.context.path}")
	private String loginContextPath;
	
	@Override
	public void commence(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException authException)
			throws IOException, ServletException {
		if(RequestVariablesBean.isAjaxRequest(request)){
			response.setStatus(403);
			response.getWriter().write("");
		} else {
			String loginUrl = getLoginFormUrl();
			if(!loginUrl.startsWith("/")){
				loginUrl = "/"+loginUrl;
			}
			response.sendRedirect("/"+loginContextPath+loginUrl);
		}
	}
	
	
}
